const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
let mainWindow;

function createWindow() {
    mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        minWidth: 300,
        minHeight: 200,
        frame: true,
        icon: app.isPackaged ? path.join(__dirname, 'build/icon.icns') : path.join(__dirname, 'images/timer-icon.png'),
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: true,
            preload: require('path').join(__dirname, 'preload.js')

        }
    });

    mainWindow.loadFile('index.html');
    //mainWindow.webContents.openDevTools();

    let isMinimized = false;

    // 监听最小化请求
    ipcMain.on('minimize-window', () => {
        isMinimized = true;
        mainWindow.setSize(300, 200);
    });

    // 监听恢复请求
    ipcMain.on('restore-window', () => {
        isMinimized = false;
        mainWindow.setSize(800, 600);
    });

    mainWindow.on('unmaximize', () => {
        if (!isMinimized) {
            mainWindow.setSize(800, 600);
        }
    });

    // 监听窗口大小变化
    mainWindow.on('resize', () => {
        const [width, height] = mainWindow.getSize();
        mainWindow.webContents.send('window-size-changed', { width, height });
    });
}

app.whenReady().then(createWindow);

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
        createWindow();
    }
});